package com.timguan.render.dao.manager.cms;

import com.timguan.render.dao.domain.cms.CmsPageDO;
import com.timguan.render.dao.domain.cms.CmsPageDOExample;
import com.timguan.render.dao.domain.cms.CmsPageDOWithBLOBs;
import com.timguan.render.dao.persistence.cms.CmsPageDOMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by guankaiqiang on 2015/3/18.
 */
public class CmsPageManagerImpl {
    @Resource
    private CmsPageDOMapper cmsPageDOMapper;

    public int insert(CmsPageDOWithBLOBs record) {
        int id = 0;
        if (record == null) {
            return id;
        }
        List<CmsPageDO> cmsPageDOs = null;
        if (record.getPageName() != null && record.getPageName().length() != 0) {
            CmsPageDOExample condition = new CmsPageDOExample();
            condition.createCriteria().andPageNameEqualTo(record.getPageName());
            cmsPageDOs = cmsPageDOMapper.selectByExample(condition);
        }
        if (cmsPageDOs == null || cmsPageDOs.size() == 0) {
            Date now = new Date();
            record.setGmtCreate(now);
            record.setGmtModified(now);
            record.setIsDelete(0);
            cmsPageDOMapper.insert(record);
            return record != null ? record.getId() : 0;
        } else {
            throw new RuntimeException("pageName " + record.getPageName() + " exist!");
        }
    }

    public int countByExample(CmsPageDOExample condition) {
        int count = 0;
        if (condition == null) {
            return count;
        }
        count = cmsPageDOMapper.countByExample(condition);
        return count;
    }

    public List<CmsPageDO> selectByExample(CmsPageDOExample condition) {
        List<CmsPageDO> cmsPageDOs = null;
        if (condition == null) {
            return cmsPageDOs;
        }
        cmsPageDOs = cmsPageDOMapper.selectByExample(condition);
        return cmsPageDOs;
    }

    public CmsPageDOWithBLOBs selectByPrimaryKey(int pageId) {
        return cmsPageDOMapper.selectByPrimaryKey(pageId);
    }

    public CmsPageDO selectByPageName(String pageName) {
        CmsPageDOExample cmsPageDOExample = new CmsPageDOExample();
        cmsPageDOExample.createCriteria().andPageNameEqualTo(pageName).andIsDeleteEqualTo(0);
        List<CmsPageDO> cmsPageDOs = cmsPageDOMapper.selectByExample(cmsPageDOExample);
        if (cmsPageDOs != null && cmsPageDOs.size() > 0) {
            return cmsPageDOs.get(0);
        }
        return null;
    }

    public CmsPageDOWithBLOBs selectPageDOWithBlobByPageName(String pageName) {
        CmsPageDOExample cmsPageDOExample = new CmsPageDOExample();
        cmsPageDOExample.createCriteria().andPageNameEqualTo(pageName).andIsDeleteEqualTo(0);
        List<CmsPageDOWithBLOBs> cmsPageDOs = cmsPageDOMapper.selectByExampleWithBLOBs(cmsPageDOExample);
        if (cmsPageDOs != null && cmsPageDOs.size() > 0) {
            return cmsPageDOs.get(0);
        }
        return null;
    }

    public int updateByPrimaryKeySelective(CmsPageDOWithBLOBs record) {
        int count = 0;
        if (record == null) {
            return count;
        }
        Date now = new Date();
        record.setGmtModified(now);
        count = cmsPageDOMapper.updateByPrimaryKeySelective(record);
        return count;
    }
}
